/*
 * Utils
 * 
 * Copyright 2008-2009 Centro Andaluz de Innovación y Tecnologías de la
 * Información y las Comunicaciones (CITIC). All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 *
 * Please contact CITIC - Edificio CITIC, C/ Marie Curie, 6 Parque
 * Tecnológico de Andalucía 29590 - Campanillas, Málaga, Spain
 * or visit www.citic.es, if you need additional information or
 * have any questions.
 */

package es.citic.puedes.weatherservice.utils;

import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import es.citic.puedes.weatherservice.WeatherException;
import es.citic.puedes.weatherservice.model.Language;

public class Utils {

	private static Logger logger = LoggerFactory.getLogger(Utils.class);

	/**
	 * converts a date to a String date with the correct date format depending
	 * on the language.
	 * 
	 * @param date
	 *            Date object to convert to a String
	 * @param language
	 *            language for choosing the correct date format
	 * @return a String object with the date with the correct format
	 * @throws WeatherException
	 */

	public static String convertDateToStringLanguageDate(Date date,
			Language language) throws WeatherException {

		logger.debug("Enter: convertDateToStringLanguageDate()");

		String formatDate = null;

		if (date != null) {

			String tempDate = date.toString();
			logger.debug("tempDatetempDatetempDate  " + tempDate);

			switch (language.getValue()) {

			// spanish
			case 1:
				// tempDate.s
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(date);
				int month = calendar.get(Calendar.MONTH);
				int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
				int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
				int year = calendar.get(Calendar.YEAR);

				// converts the date to a Spanish format date String
				formatDate = convertDateToSpanishFormatDate(dayOfWeek,
						dayOfMonth, month, year);

				break;

			// english
			case 2:
				try {
					formatDate = tempDate.substring(0, 10);
				} catch (Exception e) {
					throw new WeatherException(
							"could not convert the date to String: "
									+ e.getMessage());
				}
				break;

			default:
				try {
					formatDate = tempDate.substring(0, 10);
				} catch (Exception e) {
					throw new WeatherException(
							"could not convert the date to String: "
									+ e.getMessage());
				}
				break;
			}
		} else {
			throw new WeatherException("the date is not correct");
		}
		logger.debug("formatDateformatDateformatDate  " + formatDate);
		logger.debug("Exit: convertDateToStringLanguageDate()");
		return formatDate;
	}

	/**
	 * converts a date to a Spanish format date String. Creation date 16/02/2011
	 * 
	 * @param dayOfWeek
	 *            date day in week
	 * @param dayOfMonth
	 *            date day in month
	 * @param month
	 *            date month
	 * @param year
	 *            date year
	 * 
	 * @return a String object with the Spanish format date
	 * @throws WeatherException
	 */
	private static String convertDateToSpanishFormatDate(int dayOfWeek,
			int dayOfMonth, int month, int year) throws WeatherException {

		logger.debug("Enter: convertDateToSpanishFormatDate()");

		String spanishDate = "";

		switch (dayOfWeek) {

		case Calendar.MONDAY:
			spanishDate = "Lun";
			break;
		case Calendar.TUESDAY:
			spanishDate = "Mar";
			break;
		case Calendar.WEDNESDAY:
			spanishDate = "Mie";
			break;
		case Calendar.THURSDAY:
			spanishDate = "Jue";
			break;
		case Calendar.FRIDAY:
			spanishDate = "Vie";
			break;
		case Calendar.SATURDAY:
			spanishDate = "Sab";
			break;
		case Calendar.SUNDAY:
			spanishDate = "Dom";
			break;

		default:

			throw new WeatherException(
					"Could not convert the date to Spanish format: day of the week value is not correct");
		}

		switch (month) {

		case Calendar.JANUARY:
			spanishDate = spanishDate + " Ene";
			break;
		case Calendar.FEBRUARY:
			spanishDate = spanishDate + " Feb";
			break;
		case Calendar.MARCH:
			spanishDate = spanishDate + " Mar";
			break;
		case Calendar.APRIL:
			spanishDate = spanishDate + " Abr";
			break;
		case Calendar.MAY:
			spanishDate = spanishDate + " May";
			break;
		case Calendar.JUNE:
			spanishDate = spanishDate + " Jun";
			break;
		case Calendar.JULY:
			spanishDate = spanishDate + " Jul";
			break;
		case Calendar.AUGUST:
			spanishDate = spanishDate + " Ago";
			break;
		case Calendar.SEPTEMBER:
			spanishDate = spanishDate + " Sep";
			break;
		case Calendar.OCTOBER:
			spanishDate = spanishDate + " Oct";
			break;
		case Calendar.NOVEMBER:
			spanishDate = spanishDate + " Nov";
			break;
		case Calendar.DECEMBER:
			spanishDate = spanishDate + " Dic";
			break;

		default:

			throw new WeatherException(
					"Could not convert the date to Spanish format: month value is not correct");
		}

		if (dayOfMonth > 0 && dayOfMonth < 32) {
			spanishDate = spanishDate + " " + dayOfMonth;
		} else {

			throw new WeatherException(
					"Could not convert the date to Spanish format: day of the month value is not correct");

		}

		logger.debug("Exit: convertDateToSpanishFormatDate()");
		return spanishDate;
	}
}
